#pragma once
#include "stdafx.h"
#include "StringPraser.h"

namespace Utils
{
    void Utils::RemoveFloatStringTailingZeros(std::string &strNum)
    {
        auto pos = strNum.find_last_of(".");
        if(pos != std::string::npos) {
            while(strNum.back() == '0') {
                strNum.pop_back();
            }
            if(strNum.size() && strNum.back() == '.') {
                strNum.push_back('0');
            }
        }
    }

    void Utils::RemoveFloatStringTailingZeros(std::wstring &strNum)
    {
        auto pos = strNum.find_last_of(L".");
        if(pos != std::string::npos) {
            while(strNum.back() == L'0') {
                strNum.pop_back();
            }
            if(strNum.size() && strNum.back() == L'.') {
                strNum.push_back(L'0');
            }
        }
    }

    std::vector<std::string> &SplitString(const std::string &s, std::vector<std::string> &elems, char delim)
    {
        std::stringstream ss(s);
        std::string item;
        while(std::getline(ss, item, delim)) {
            elems.push_back(item);
        }
        return elems;
    }

    std::vector<std::string> SplitString(const std::string &s, char delim)
    {
        std::vector<std::string> elems;
        return SplitString(s, elems, delim);
    }

    std::vector<std::string> &SplitString(const std::string &s, std::vector<std::string> &elems, const std::string &delims)
    {
        char *tok;
        char *cchars = (char *)malloc(s.size() + 1);
        char *cstr = cchars;
        strcpy(cstr, s.c_str());
        tok = strtok(cstr, delims.c_str());
        while(tok != NULL) {
            elems.push_back(tok);
            tok = strtok(NULL, delims.c_str());
        }
        free(cchars);
        return elems;
    }

    std::vector<std::string> SplitString(const std::string &s, const std::string &delims)
    {
        std::vector<std::string> elems;
        return SplitString(s, elems, delims);
    }

    std::vector<std::wstring> &SplitString(const std::wstring &s, std::vector<std::wstring> &elems, wchar_t delim)
    {
        std::wstringstream ss(s);
        std::wstring item;
        while(std::getline(ss, item, delim)) {
            elems.push_back(item);
        }
        return elems;
    }

    std::vector<std::wstring> SplitString(const std::wstring &s, wchar_t delim)
    {
        std::vector<std::wstring> elems;
        return SplitString(s, elems, delim);
    }

    std::vector<std::wstring> &SplitString(const std::wstring &s, std::vector<std::wstring> &elems, const std::wstring &delims)
    {
        wchar_t *tok;
        wchar_t *cchars = (wchar_t *)malloc((s.size() + 1) * sizeof(std::wstring::value_type));
        wchar_t *cstr = cchars;
        wcscpy(cstr, s.c_str());
        tok = wcstok(cstr, delims.c_str());
        while(tok != NULL) {
            elems.push_back(tok);
            tok = wcstok(NULL, delims.c_str());
        }
        free(cchars);
        return elems;
    }

    std::vector<std::wstring> SplitString(const std::wstring &s, const std::wstring &delims)
    {
        std::vector<std::wstring> elems;
        return SplitString(s, elems, delims);
    }
} // namespace Utils